{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d768e79d",
   "metadata": {},
   "source": [
    "## 7.4 池化层(pooling layer)\n",
    "\n",
    "卷积神经网络只有卷积层这一种结构吗？不是的，下面我们就要介绍池化层。池化层是用来压缩信息的层。它将输入的多个特征映射到一个更小的特征空间中，从而使网络变得更加紧凑。\n",
    "\n",
    "如果没有池化层，网络的参数数量就会变得非常大，这会导致训练时间变长，并且容易出现过拟合现象。池化层通过下采样的方式实现了信息的压缩，减少了网络的参数数量，这有助于缩短训练时间，并且可以防止过拟合。这里，下采样是指将输入的特征缩小为原来的一部分。此外，池化层还可以帮助网络学习更加抽象的特征，从而提升分类的准确率。总体而言，池化层在卷积神经网络中起到了信息压缩的作用，减少了参数的数量，防止过拟合，并且可以帮助网络学习更加抽象的特征。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5960d8e7",
   "metadata": {},
   "source": [
    "### 7.4.1 常见池化方法\n",
    "\n",
    "常见的下采样方法有最大池化和平均池化。最大池化层会选取输入特征中的最大值作为输出特征，平均池化层则是将输入特征求平均值作为输出特征。咱们分别用具体示例来加以说明其基本原理和具体步骤。\n",
    "\n",
    "先用代码实现最大池化层，它将输入张量的每个 2x2 的窗口中的最大值取出来，然后组成一个新的 2x2 的张量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cc41a223",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAACmCAYAAAB5qlzZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAZnUlEQVR4nO3deXxU9b3/8deZmUw2spGVkEDYZV8DBb2RXoJVXK5Fr6L+rg+11vooirePS+t2b9V69ae2tVav0IKiXK/VYturRQUVF0A2A8guO4QshGwkk30yM+f+MTHNMqQuMyc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ph16/Cmp+MePfqcYxxnzuAZMKD92JORgb3DJtKhEvncc8RdcAExd9yBEaArrO30aXwDB7Yf+zIzMU6fDm1R35D2KCLhJGShW7pwIXvfeovqSy4hddWqL3ZTCNtqGE1NJC5ZQvVPftLzwB62ugyVlltuwbVzJ3UbNuDLyCD63//9i9UV6jYkHduj/PKX/r1Ju9bRB9qjiISTkH96ofrii0l6//1u561uQ+I4dYqIoiKyLr2U7Lw8HGVlZF1xBfYuP+TzZGTg6DCDdJSV4e0wIw8FMy0N7Haw2XDfeCOOnTu7jfFlZmLrMIO0lZZinqttd7Ccqz1K1zFh3h5FJJyEJHQjT51q/zph/Xqac3K6jbG6DUnrqFEUFhRQtGEDRRs24MnIoPivf8XbJehbJkwg4uRJHEVF4HYT++abNMyZE7K6AIyysvavI958E2+A5Q/vlCnYjh/HVlgIbjcRf/kL7gCfzAiqb0h7FJFw8rW7AQ+57z7iduzAUVPD+HnzKL3tNhI2bSKqsBDTZsM9YACn7r0X8LchGfzwwxx9+ulObUgMr5fKK64IahuStLvuImrbNuxnzzLo/PM5e9dd1HX4WFtH9jNnSL33XspWrACHg8oHHiDjppswfD7qrr6a1h5aZH9ZsbfeimPTJoyqKhLGjqXpnntwbNqEY+9eTMPAN2gQjU8+CYBx+jSxd91F/apV4HDQ+MQT9Lv6avB6cd9wA74e1qa/tOuug48+8s9ks7LgoYf8HyO75Rb/x8iczs7tUW69Fd5+u3N7FK/XPz7M2qOIhBO16wmicK0Lwro2tevpA9Su5ytRux4Rkd6m0BURsZBCV0TEQgpdERELKXRFRCyk0BURsZBCV0TEQgpdERELKXRFRCyk0BURsZBCV0TEQgpdERELKXRFRCyk0BURsZBCV0TEQj3upysiIsGlma6IiIUUuiIiFlLoiohYSKErImIhha6IiIUUuiIiFvo/59WyEyR0zOMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 创建一个 4x4 的二维张量，每一行代表一个图像的一行像素\n",
    "input_tensor = torch.tensor([\n",
    "    [1, 2, 3, 4],\n",
    "    [5, 6, 7, 8],\n",
    "    [9, 10, 11, 12],\n",
    "    [13, 14, 15, 16]\n",
    "])\n",
    "\n",
    "# 将输入张量转化为浮点数类型的张量\n",
    "input_tensor = input_tensor.to(torch.float)\n",
    "\n",
    "# 将输入张量转化为四维张量，因为 MaxPool2d 需要接受四维的输入\n",
    "input_tensor = input_tensor.unsqueeze(0).unsqueeze(0)\n",
    "\n",
    "# 将输入张量传入最大池化层，指定池化窗口的大小为 2x2，步长为 2\n",
    "max_pooling_layer = torch.nn.MaxPool2d(kernel_size=2, stride=2)\n",
    "output = max_pooling_layer(input_tensor)\n",
    "\n",
    "# 使用 Matplotlib 绘制输入张量和输出张量\n",
    "fig, axs = plt.subplots(1, 2)\n",
    "\n",
    "# 绘制输入张量\n",
    "axs[0].axis('off')\n",
    "axs[0].imshow(input_tensor[0, 0].detach().numpy(),cmap=\"gray\")\n",
    "\n",
    "# 在图像上为每个像素添加像素值的标签，使用红色显示\n",
    "for i in range(input_tensor.shape[2]):\n",
    "    for j in range(input_tensor.shape[3]):\n",
    "        axs[0].text(j, i, input_tensor[0, 0, i, j].item(), ha=\"center\", va=\"center\", color=\"r\")\n",
    "\n",
    "# 绘制输出张量\n",
    "axs[1].axis('off')\n",
    "axs[1].imshow(output[0, 0].detach().numpy(),cmap=\"gray\")\n",
    "\n",
    "# 在图像上为每个像素添加像素值的标签，使用红色显示\n",
    "for i in range(output.shape[2]):\n",
    "    for j in range(output.shape[3]):\n",
    "        axs[1].text(j, i, output[0, 0, i, j].item(), ha=\"center\", va=\"center\", color=\"r\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee339316",
   "metadata": {},
   "source": [
    "在上文的代码中，我们对输入张量使用了一个最大池化层，指定了池化窗口的大小为 2x2，步长为 2。在这种情况下，最大池化层会在输入张量的每个通道中持续滑动一个 2x2 的窗口，并选取窗口中的最大值，作为输出张量对应位置的值。例如，在输入张量中，(0,0)位置的子区域为\\begin{bmatrix}1&2&5&6\\end{bmatrix}最大值为6，因此输出张量的(0,0)位置的值为6。池化窗口以步长2继续滑动，(0,1)位置的子区域为\\begin{bmatrix}3&4&7&8\\end{bmatrix}最大值为8，因此输出张量的(0,1)位置的值为8。以此类推...\n",
    "\n",
    "再来看看平均池化操作的情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5447409f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAACmCAYAAAB5qlzZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAalUlEQVR4nO3deXRUZZ7/8fetquxkgSyEkEAgLLKvgRYdpBtwQXEUHdvW33hs21Z/jUt7ftpu09Muo+PWdo+20ILa0o5LQy/uIOIGshn2TSBsISSE7KmkslSq6v7+qBhTSYGKVTcV+/M6h0Pde5976ktIPnnyVOX5GqZpIiIi1rB1dwEiIv9MFLoiIhZS6IqIWEihKyJiIYWuiIiFFLoiIhZynOqiYRgR+X6y3r17d3cJQUVqXRC5tW3atMnojueN1M/tSDVp0qTuLqHHOdnntma6IiIWUuiKiFhIoSsiYiGFroiIhRS6IiIWUuiKiFhIoSsiYiGFroiIhRS6IiIWUuiKiFhIoSsiYiGFroiIhRS6IiIWCmnovgCcAHaeYsz/AIXAdmBCKJ/8FPr7fLxZX8+GujrW1dVxY3Nz10GmyX83NrKpro41TidjPZ6w1xXt8/H3khLeOXaM5cXF3FZdHbSu/6ys5KOjR3n32DFGtbSEva4v2UyTV/bs4XcHDgSt646jR/nHrl28tmcPwxsbLatLpCcLaei+BJx/iusXAEPb/twAL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8wk7j/+45vVFe7GfB4P1NTAhg3wxBNwxRVd6+iOukR6sLC/e6H6/PPp/eGHXc63ZmQQ1WEGGV1e3t6WJBwcR48SVVxM9oUXkjN9Oo6yMrIvvhh7pxf5PJmZODrMIB1lZXg7zMjDwczIALsdbDbc11yDI0gPLF9WFrYOM0hbaSlmZmZY6yI7G+bN84folCn+zaMrK7uOKS7+6vjYsa/apYhIF2EJ3ZijR9sfJ3/6Kc25uV3GuEaOJLa4mOiSEozWVnqvXEltGPsYtQ4fTlFBAcWrV1O8ejWezEyOvfUW3k5B3zJ2LFFHjuAoLga3m4R33sE1c2bY6gIwysraH0e98w7eIMsf3okTsR06hK2oCNxuov7+d9zh3hH/kkvgo4/8j/fvB7cb0tICx+Tn+9uoHD7sv/7663DxxeGtS6QH+87teoK1IUleu5bYoiJMmw13v34cvecegIA2JDgcHL3zTobecguG10vlxRfTHMLeTxm33Ubsxo3Ya2oYcNZZ1Nx2G/Ud3tbWkf3ECdLvuYeyF18Eh4PK3/yGzGuvxfD5qL/8clqHDQtZXQnXX49j7VqMqiqSR42i6e67caxdi2PnTkzDwDdgAI1PPQWAcfw4CbfdRsPSpeBw0Pj44/S6/HLwenFffTW+U6xNf2vB2qNcd53/z+jR/hfJlizxz3o7tkdxOPxdWc87z/9Ohuuu87+9TESCUrueEIrUuiCia1O7nh5A7XpOi9r1iIh0N4WuiIiFFLoiIhZS6IqIWEihKyJiIYWuiIiFFLoiIhZS6IqIWEihKyJiIYWuiIiFFLoiIhZS6IqIWEihKyJiIYWuiIiFFLoiIhY65X66IiISWprpiohYSKErImIhha6IiIUUuiIiFlLoiohYSKErImKh/w906HgFNzj+HQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 将输入张量传入平均池化层，指定池化窗口的大小为 2x2，步长为 2\n",
    "avg_pooling_layer = torch.nn.AvgPool2d(kernel_size=2, stride=2)\n",
    "output = avg_pooling_layer(input_tensor)\n",
    "\n",
    "# 使用 Matplotlib 可视化平均池化层的输入张量和输出张量\n",
    "fig, axs = plt.subplots(1, 2)\n",
    "\n",
    "# 绘制输入张量\n",
    "axs[0].axis('off')\n",
    "axs[0].imshow(input_tensor[0, 0, :, :].detach().numpy(), cmap=\"gray\")\n",
    "\n",
    "# 在图像上为每个像素添加像素值的标签，使用红色显示\n",
    "for i in range(input_tensor.shape[2]):\n",
    "    for j in range(input_tensor.shape[3]):\n",
    "        axs[0].text(j, i, input_tensor[0, 0, i, j].item(), ha=\"center\", va=\"center\", color=\"r\")\n",
    "        \n",
    "# 绘制输出张量\n",
    "axs[1].axis('off')\n",
    "axs[1].imshow(output[0, 0, :, :].detach().numpy(), cmap=\"gray\")\n",
    "\n",
    "# 在图像上为每个像素添加像素值的标签，使用红色显示\n",
    "for i in range(output.shape[2]):\n",
    "    for j in range(output.shape[3]):\n",
    "        axs[1].text(j, i, output[0, 0, i, j].item(), ha=\"center\", va=\"center\", color=\"r\")\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f919f947",
   "metadata": {},
   "source": [
    "在平均池化层中，输入张量与输出张量的结构与最大池化层相同，只是平均池化层在处理时，会将输入张量中每个2x2的子区域的值取平均值作为输出张量中相应位置的值。例如，在输入张量中，(0,0)位置的子区域为\\begin{bmatrix}1&2&5&6\\end{bmatrix}平均值为$\\frac{1+2+5+6}{4}=\\frac{14}{4}=3.5$，因此输出张量的(0,0)位置的值为3.5。\n",
    "\n",
    "总的来说，最大池化层可以帮助网络学习更强的特征，而平均池化层可以帮助网络学习更平滑的特征。二者都可以帮助网络学习更强的特征，同时还可以防止过拟合。当然，池化层可能会丢失某些有用的信息，并且对噪声敏感。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00e2a0cc",
   "metadata": {},
   "source": [
    "### 7.4.2 池化填充和步长"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d124f1a6",
   "metadata": {},
   "source": [
    "在池化操作中，填充和步长（也可叫做步幅）是两个重要的概念，它们可以帮助控制池化层的输出大小。\n",
    "\n",
    "填充是在池化运算之前添加的一些行和列，用于填充输入的边界。这有助于控制池化层的输出大小。例如，在池化层后使用全连接层时，可以使用填充来确保输入和输出大小相同。\n",
    "\n",
    "步长是指在池化运算期间每次滑动的窗口的距离。步长越大，窗口在输入图像上的滑动距离就越大，这意味着输出的特征图就越小。步长越小，窗口在输入上的滑动距离就越小，这意味着输出的特征图就越大。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bd36e86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAACmCAYAAAB5qlzZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8+yak3AAAACXBIWXMAAAsTAAALEwEAmpwYAAAfeElEQVR4nO3deXxU9b3/8dcsmcnMJJN9DxB2CHvYZDEoiwUUUNsqgqWtC3qLS338fFRLbxe1Km2tt7feShVcuIp4cSkWBKxSKTsNIGuEoEIgG9kzycxkJjNzfn9MjJNkggIzJwN+no8HD3LOfM9jPobxPd/5fr9zvhpFURBCCKEObXcXIIQQ3yYSukIIoSIJXSGEUJGErhBCqEhCVwghVCShK4QQKtKf70GNRhOR68kSEhK6u4SgIrUuiNza9u3bp+mO543U1/bFyMjI6O4SQiYzM7O7SwiZrl7b0tMVQggVSegKIYSKJHSFEEJFErpCCKEiCV0hhFCRhK4QQqhIQlcIIVQkoSuEECqS0BVCCBVJ6AohhIokdIUQQkUSukIIoSIJXSGEUNF57zJ2oV4CbgAqgWFdtPlvYDbgAH4EfBLKArqQ5fPxvN1Oqs+HAqwyGnkhOrp9I0XhaaeTGS0tODUalpjNHNaH9NfTicHn483ycgyKgk5R2Gyx8N+Jie3bKArP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qp9+Gl98PPX33UfWjTcCUHf//W2TaqFguesu9Dt3oqmpIW7IEJyPPop+5070R46gaDT4evbE8eyzAGjKy7E8+CBNa9eCXo/j978n5nvfA68X98KF+M4zNn3BbrsNtm7192Szs+Gxx/wzv3fc4Z/5NRjaz/zedZd/IiJw5tfr9bcP48yvEJc7jXKeMbr9+/dH5ABepO5sG6l1QUTXFvxGBWEWqa/ti5HZxfLHy1FGJH7B4uIFfW3LN9KEEEJFErpCCKEiCV0hhFCRhK4QQqhIQlcIIVQkoSuEECqS0BVCCBVJ6AohhIokdIUQQkUSukIIoSIJXSGEUJGErhBCqEhCVwghVCShK4QQKpLQFUIIFZ33frpCCCFCS3q6QgihIgldIYRQkYSuEEKoSEJXCCFUJKErhBAqktAVQggV/X/1P9l5JWlJhwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 创建一个 4x4 的二维张量，每一行代表一个图像的一行像素\n",
    "input_tensor = torch.tensor([\n",
    "    [1, 2, 3, 4],\n",
    "    [5, 6, 7, 8],\n",
    "    [9, 10, 11, 12],\n",
    "    [13, 14, 15, 16]\n",
    "])\n",
    "\n",
    "# 将输入张量转化为浮点数类型的张量\n",
    "input_tensor = input_tensor.to(torch.float)\n",
    "\n",
    "# 将输入张量转化为四维张量，因为 MaxPool2d 需要接受四维的输入\n",
    "input_tensor = input_tensor.unsqueeze(0).unsqueeze(0)\n",
    "\n",
    "# 进行池化操作，使用2*2的滑动窗口，步幅为1，填充为0\n",
    "max_pooling_layer = torch.nn.MaxPool2d(kernel_size=2, stride=1, padding=0)\n",
    "output = max_pooling_layer(input_tensor)\n",
    "\n",
    "# 使用 Matplotlib 绘制输入张量和输出张量\n",
    "fig, axs = plt.subplots(1, 2)\n",
    "\n",
    "# 绘制输入张量\n",
    "axs[0].axis('off')\n",
    "axs[0].imshow(input_tensor[0, 0].detach().numpy(),cmap=\"gray\")\n",
    "\n",
    "# 在图像上为每个像素添加像素值的标签，使用红色显示\n",
    "for i in range(input_tensor.shape[2]):\n",
    "    for j in range(input_tensor.shape[3]):\n",
    "        axs[0].text(j, i, input_tensor[0, 0, i, j].item(), ha=\"center\", va=\"center\", color=\"r\")\n",
    "\n",
    "# 绘制输出张量\n",
    "axs[1].axis('off')\n",
    "axs[1].imshow(output[0, 0].detach().numpy(),cmap=\"gray\")\n",
    "\n",
    "# 在图像上为每个像素添加像素值的标签，使用红色显示\n",
    "for i in range(output.shape[2]):\n",
    "    for j in range(output.shape[3]):\n",
    "        axs[1].text(j, i, output[0, 0, i, j].item(), ha=\"center\", va=\"center\", color=\"r\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd2fc65d",
   "metadata": {},
   "source": [
    "填充（padding）指的是在图像的边缘外添加额外的像素，以便滑动窗口能够覆盖整个图像。步长（stride）指的是滑动窗口在图像上每次移动的距离。在上面的代码中，我们使用了填充值为0的池化层，因此在输入图像的边缘没有添加任何像素。我们也使用了步长值为1的池化层，因此滑动窗口每次都只移动一个像素。这意味着我们的滑动窗口在图像上每个位置都有一次机会，并且输出图像的大小和输入图像的大小相同。如果我们将填充值设置为1，则输出图像将比输入图像大2个像素；如果将步长值设置为2，则输出图像将比输入图像小2个像素。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e600ffb7",
   "metadata": {},
   "source": [
    "### 7.4.3 池化多通道问题\n",
    "\n",
    "在多个输入通道的情况下，池化层的输入称为多通道数据。池化层可以在每个通道上单独进行池化，也可以在所有通道上进行池化，并将池化结果在通道维度上进行拼接。\n",
    "\n",
    "在多通道数据上进行池化时，通常使用同样的池化方式。例如，如果使用的是最大池化，则池化层会在每个通道上取出每个池化窗口内的最大值，然后将这些最大值拼接在一起。\n",
    "\n",
    "在处理多通道数据时，池化层通常不会对通道维度进行扩展，也不会对通道维度进行下采样。因此，池化层的输出通道数与输入通道数相同。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7da10a05",
   "metadata": {},
   "source": [
    "[Next 7-5 卷积神经网络](./7-5%20卷积神经网络.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12c0c1cc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
